--[[
-- @Author:      feilong
-- @DateTime:    2020-11-13 23:05:48
-- @Description: http请求
-- @Example:



--]]
local skynet = require("skynet")
local httpc = require "http.httpc"
local cjson = require "cjson"
local md5 = require "md5"

local server_config = dofile(skynet.getenv("server_config"))

local table_remove = table.remove
local table_insert = table.insert


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor()
	


	self:register()
end

--注册本服务里的消息
function MessageHandler:register()
	self.message_dispatch = g.MsgDis()
	self.message_dispatch:register("start", handler(self, self.start))
	self.message_dispatch:register("post", handler(self, self.post))
end



---------------------------------------------------------
-- CMD
---------------------------------------------------------

function MessageHandler:start()
	return true
end

--post
function MessageHandler:post(url, data, exheader)
    exheader = exheader or {}
    local exchange_url = url
    local host_name = self.host_name
    print("_________web_post__", url, data)

    local uid = exheader.uid or -1
    local effect_time = util.get_now_time()
    local sign = md5.sumhexa(string.format("%d%d%s",uid, effect_time, constant.internal_key or ''))
    local headers = {
        sign = sign,
        uid = uid,
        time = effect_time,
        ['HTTP-CLIENT-IP'] = util.inet_ntoa(exheader.ip or 0),
        ['Client-ChannelId']= exheader.channel,
        ['Client-VersionId'] = exheader.client_version,
        ['Client-Device'] = exheader.device_brand,
        ['Client-MachineCode'] = exheader.device_id,
        ['account-id'] = exheader.account_id or -1,
        ['Client-System'] = exheader.os_system or '',
        ['Client-Platform'] = exheader.platfrom or '',
        ['content-type'] = exheader.content_type or 'application/json',
        ['Current-System'] = exheader.current_system or '',

    }

    local status,body = httpc.request('POST', host_name, exchange_url, nil, headers,data)
    print("requst-->web_post status:",status,", url:", host_name, exchange_url,", body",tostring_r(data),", header", headers)
    if status ~= 200 then
        g.Log():error('failed to web_proxy',status, body)
        return status, nil
    end
    if not body then
        g.Log():error('web_proxy invalid response', body)
        return -100, nil
    end

    print("relay-->web_post url:", host_name,exchange_url,",body",body)
    return status, body
end


--get



return MessageHandler